/*
 * @Author: 肖芬芳 1911683843@qq.com
 * @Date: 2022-12-02 11:00:36
 * @LastEditors: 肖芬芳 1911683843@qq.com
 * @LastEditTime: 2022-12-02 14:34:22
 * @FilePath: \blog\routes\admin\system\role\deleteRight.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { ObjectId } = require('mongodb');
const { Role } = require('../../../model/role.js');
module.exports = async (req, res) => {
  let roleId = req.param('roleId')
  let rightId = req.param('rightId')
  let role = await Role.findOne({ _id: ObjectId(roleId) })
  let right = role.children
  let arr = []
  // 删除树状结构的结点数据
  const removeNodeInTree = (treeList, id) => {
    if (!treeList || !treeList.length) {
      return
    }
    for (let i = 0; i < treeList.length; i++) {
      if (treeList[i].id === id) {
        treeList.splice(i, 1);
        break;
      }
      removeNodeInTree(treeList[i].children, id)
    }
  }
  removeNodeInTree(right, rightId)
  // console.log(right)
  await Role.updateOne({ _id: ObjectId(roleId) }, { $set: { children: right } })
  res.send({ 'code': 0, 'data': right });
}